package com.bls.cate.ui.fragment.article;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.bls.blslib.base.BaseFragment;
import com.bls.blslib.eventbus.Event;
import com.bls.cate.R;
import com.bls.cate.adapter.ArticleListAdapter;
import com.bls.cate.constant.EventBusCode;
import com.bls.cate.constant.Interface;
import com.bls.cate.model.ArticleListRes;
import com.bls.cate.ui.activity.read_article.ReadArticleActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/2/7.
 */

public class ArticleFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    private SwipeRefreshLayout slRefresh;
    private RecyclerView rvList;

    private List<ArticleListRes.ResultListDataBean> articleList;
    private ArticleListAdapter articleListAdapter;


    public static ArticleFragment newInstance(String param1, String param2) {
        ArticleFragment userFrag = new ArticleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        userFrag.setArguments(args);
        return userFrag;
    }

    @Override
    protected void initRoot() {

    }

    @Override
    protected void initParams() {
        articleList = new ArrayList<>();
        articleListAdapter = new ArticleListAdapter(articleList);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_article;
    }

    @Override
    protected void initFvb(View fragView) {
        slRefresh = fragView.findViewById(R.id.sl_refresh);
        rvList = fragView.findViewById(R.id.rv_list);
    }

    @Override
    protected void initView() {
        slRefresh.setRefreshing(false);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        rvList.setLayoutManager(layoutManager);
        rvList.setAdapter(articleListAdapter);
    }

    @Override
    protected void initListener() {
        slRefresh.setEnabled(true);
        slRefresh.setOnRefreshListener(this);
        articleListAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        presenter.requestStrData(Interface.INTERFACE_CODE_GET_ARTICLE_LIST, Interface.INTERFACE_ARTICLE, CacheMode.NO_CACHE, new HttpParams("RequestMethod", "ArticleList"));
    }

    @Override
    protected void initOnResume() {

    }

    @Override
    protected void initOnPause() {

    }

    @Override
    protected void initOnHiddenChanged(boolean hidden) {
        if (!hidden)
            initData();
    }

    @Override
    public void showOnSuccess(int requestId, String responseBody, Response response) {
        if (requestId == Interface.INTERFACE_CODE_GET_ARTICLE_LIST) {
            ArticleListRes res = new Gson().fromJson(responseBody, ArticleListRes.class);
            if (res.isResultStatus()) {
                articleList = res.getResultListData();
                articleListAdapter.setNewData(articleList);
            }
        }
    }

    @Override
    public void showOnFinish(int requestId) {
        super.showOnFinish(requestId);
        if (slRefresh.isRefreshing())
            slRefresh.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), ReadArticleActivity.class);
        intent.putExtra("Article", (Serializable) adapter.getItem(position));
        getActivity().startActivity(intent);
    }
}
